package websocket;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;

import db.Tootaja;
import db.Yyrnik;


@WebServlet("/chat")
public class ChatWs extends WebSocketServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected StreamInbound createWebSocketInbound(String string,
			HttpServletRequest hsr) {	
	    if(hsr.getSession().getAttribute("user") != null){
		Yyrnik p = (Yyrnik) hsr.getSession().getAttribute("user");
		return new ChatConnection(p.getKasutajanimi());
	    }else if(hsr.getSession().getAttribute("admin") != null){
		Tootaja p = (Tootaja) hsr.getSession().getAttribute("admin");
		return new ChatConnection(p.getAmet().getNimetus());
	    }else{
		return new ChatConnection(null);
	    }
	}	
}